package com.ciwong.epaper.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.me.bean.MaterialBean;
import com.ciwong.epaper.modules.me.bean.SubjectBean;

import java.util.List;


/**
 * Created by keepon on 2018/8/7.
 */
public class MaterialItemView extends RelativeLayout implements View.OnClickListener {
    TextView mTvSubjectName, mTvVersionName, mTvBookGrarde, mTvMoudle;
    LinearLayout mLlChangeMoudle, mLlChangeMaterial;
    RelativeLayout mRlStartStudy, mRlCatalogueFailed, mRlCatalogueSuccess;
    ImageView mIvSubjectBg;
    private static final String TAG = "MaetrialItemView";
    private String mUnitBeanUnitid;
    private CatalogueInfo catalogueInfo;
    private MaterialBean mSelectedMaterialBean;

    public MaterialItemView(Context context) {
        this(context, null);
    }

    public MaterialItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MaterialItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_view_new_book_desk, this);
        init();
        initEvent();
    }

    private void initEvent() {
        mLlChangeMaterial.setOnClickListener(this);
        mLlChangeMoudle.setOnClickListener(this);
        mRlStartStudy.setOnClickListener(this);
        mRlCatalogueFailed.setOnClickListener(this);
    }

    private void init() {
        mIvSubjectBg = findViewById(R.id.iv_subject_bg);
        mTvMoudle = findViewById(R.id.tv_module);
        mTvVersionName = findViewById(R.id.tv_book_name);
        mTvBookGrarde = findViewById(R.id.tv_book_grade);
        mLlChangeMoudle = findViewById(R.id.ll_change_module);
        mLlChangeMaterial = findViewById(R.id.ll_change_material);
        mRlStartStudy = findViewById(R.id.rl_start_study);
        mRlCatalogueFailed = findViewById(R.id.rl_catalogue_failed);
        mRlCatalogueSuccess = findViewById(R.id.rl_catalogue_success);
        Log.e(TAG, "init: MaetrialItemView");
    }

    public void bindData(SubjectBean subjectBean, List<CatalogueInfo> cataloguesBean) {
        Log.e(TAG, "bindBookMarkData: MaetrialItemView");
        if (subjectBean != null) {
            setSubjectIcon(subjectBean);
            List<MaterialBean> materialBeanList = subjectBean.getMaterial();
            if (materialBeanList != null) {
                for (MaterialBean materialBean : materialBeanList) {
                    if (materialBean.isSelected()) {
                        mSelectedMaterialBean = materialBean;
                        mTvVersionName.setText(getGradeBookName(materialBean)+" "+materialBean.getVersionName());
                        //mTvBookGrarde.setText(getGradeBookName(materialBean));
                        break;
                    }
                }
            }
        }

        mRlCatalogueFailed.setVisibility(GONE);
        mRlCatalogueSuccess.setVisibility(VISIBLE);
        if (null != cataloguesBean) {
            setTvModule(cataloguesBean);
        }


		/*if (cataloguesBean != null) {

			List<CatalogueUnitBean> catalogueUnitBeanList = cataloguesBean.getData();
			if (catalogueUnitBeanList != null && catalogueUnitBeanList.size() > 0) {
				mRlCatalogueFailed.setVisibility(GONE);
				mRlCatalogueSuccess.setVisibility(VISIBLE);
				setTvModule(catalogueUnitBeanList);
			} else {
				mRlCatalogueSuccess.setVisibility(GONE);
				mRlCatalogueFailed.setVisibility(VISIBLE);
			}
		} else {
			mRlCatalogueFailed.setVisibility(VISIBLE);
			mRlCatalogueSuccess.setVisibility(GONE);

		}*/
    }

    private void setTvModule(List<CatalogueInfo> catalogueUnitBeanList) {

        for (int i = 0; i < catalogueUnitBeanList.size(); i++) {
            if (null != catalogueUnitBeanList.get(i).getSelect() && catalogueUnitBeanList.get(i).getSelect()) {
                mUnitBeanUnitid = catalogueUnitBeanList.get(i).getId();
                catalogueInfo = catalogueUnitBeanList.get(i);
                mTvMoudle.setText(catalogueUnitBeanList.get(i).getName());
                return;
            } else {
                if (catalogueUnitBeanList.get(i).getChildren() != null && catalogueUnitBeanList.get(i).getChildren().size() > 0) {
                    List<CatalogueInfo> childCatalogueList = catalogueUnitBeanList.get(i).getChildren();
                    for (int j = 0; j < childCatalogueList.size(); j++) {
                        if (null != childCatalogueList.get(j).getSelect() && childCatalogueList.get(j).getSelect()) {
                            mUnitBeanUnitid = childCatalogueList.get(j).getId();
                            catalogueInfo = childCatalogueList.get(j);
                            mTvMoudle.setText(childCatalogueList.get(j).getName());
                            return;
                        }
                    }
                }
            }
        }

    }


    private void setSubjectIcon(SubjectBean subjectBean) {

        mIvSubjectBg.setBackgroundResource(R.drawable.subject_background);

		/*Glide.with(getContext()).load(subjectBean.getSubjectBgMobile()).into(new ViewTarget<View, GlideDrawable>(mIvSubjectBg) {
			@Override
			public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
				mIvSubjectBg.setBackground(resource.getCurrent());
			}

			@Override
			public void onLoadFailed(Exception e, Drawable errorDrawable) {
				mIvSubjectBg.setBackgroundResource(R.mipmap.bg_book_desk_default);
			}
		});*/
    }

    public String getGradeBookName(MaterialBean materialBean) {
        String gradeName = materialBean.getGradeName();
        return gradeName;
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_change_material) {
            if (mOnMaterItemViewClicklistener != null) {
                mOnMaterItemViewClicklistener.onClickChangeMaterial(mSelectedMaterialBean);
            }
        } else if (id == R.id.ll_change_module) {
            if (mOnMaterItemViewClicklistener != null) {
                mOnMaterItemViewClicklistener.onClickChangeMoudle(mUnitBeanUnitid);
            }
        } else if (id == R.id.rl_start_study) {
            if (mOnMaterItemViewClicklistener != null) {
                mOnMaterItemViewClicklistener.onClickStartStudy(catalogueInfo);
            }
        } else if (id == R.id.rl_catalogue_failed) {
            if (mOnMaterItemViewClicklistener != null) {
                mOnMaterItemViewClicklistener.onClickRefreshCatalogue();
            }
        }
    }

    public void setOnMaterItemViewClicklistener(OnMaterItemViewClicklistener onMaterItemViewClicklistener) {
        mOnMaterItemViewClicklistener = onMaterItemViewClicklistener;
    }

    OnMaterItemViewClicklistener mOnMaterItemViewClicklistener;

    public interface OnMaterItemViewClicklistener {
        void onClickChangeMoudle(String unitBeanUnitid);

        void onClickChangeMaterial(MaterialBean selectedMaterialBean);

        void onClickStartStudy(CatalogueInfo catalogueInfo);

        void onClickRefreshCatalogue();
    }
}
